/**
 *
 * @description: 获取图书详情
 * @author: junyong.hong
 * @createTime: 2018/6/11
 * @version: 1.0.0.0
 * @history:
 *    1、
 *    2、
 *
 */
const { mysql } = require('../qcloud')

module.exports = async (ctx) => {
    const { id } = ctx.request.query
    const detail = await mysql('books')
    .select('books.*', 'csessioninfo.user_info')
    .join('csessioninfo', 'books.openid', 'csessioninfo.open_id')
    .where('id', id)
    .first()

    const info = JSON.parse(detail.user_info)
    // 数据合并（detail里添加两个字段：nickName、image）
    ctx.state.data = Object.assign({}, detail, {
        tags: detail.tags.split(','),
        summary: detail.summary.split('\n'),
        user_info: {
            nickName: info.nickName,
            image: info.avatarUrl
        }
    })

    await mysql('books')
    .where('id', id)
    .increment('count', 1)
}
